package com.sgb.kjwl.viewmodel.home;


import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.BossEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.material.MerchantRecommendationFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author Administrator
 * by hq 2021.04.02
 */
public class MerchantRecommendationModel extends BaseViewModel {

    public HomeRepertory repertory;
    private MerchantRecommendationFragment fragment;
    private int page = 1;
    private static final int limit = 10;

    public MerchantRecommendationModel(MerchantRecommendationFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /***
     * 获取经纬度信息
     */
    public void getLocationLL() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), "", "", "").enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getLocationLL(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    /***
     *     首页商品模块-推荐供应商
     */

    public void getRecommendSupplier(double longitude, double latitude, boolean isLoadMore) {
        Map<String, Object> map = new HashMap(3);
        map.put("longitude", latitude);
        map.put("latitude", longitude);
        page = isLoadMore ? page + 1 : 1;
        map.put("limit", limit);
        map.put("page", page);
        map.put("areaCode", repertory.getAreaCode());
        APIManager.getInstance().getCommonAPI().getRecommandSupplier2(map).enqueue(new ICallBack<BaseEntity<List<BossEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<BossEntity>> data) {
                if (data.getData() == null) {
                    return;
                }
                fragment.showMerChantList(data.getData(), isLoadMore);
            }
        });
    }

}
